package com.game.src.main;

import java.awt.Graphics;
import java.util.LinkedList;
import java.util.Random;

public class Controller {

	private LinkedList<Bullet> b = new LinkedList<Bullet>();
	private LinkedList<Meteor> m = new LinkedList<Meteor>();
	private LinkedList<Explosion> e = new LinkedList<Explosion>();
	private  LinkedList<Message> messages = new LinkedList<Message>();
	private LinkedList<StrongBullet> sb = new LinkedList<StrongBullet>();

	Random r = new Random();

	private int meteors = 1;
	private int meteorhealth = 10;
	private boolean straight = true;
	private int time = 300;
	
	private StrongBullet tempStrongBullet;
	private Bullet tempBullet;
	private Meteor tempMeteor;
	private Explosion tempExplosion;

	
	Game game;
	Textures tex;

	public Controller(Game game, Textures tex) {
		this.game = game;
		this.tex = tex;
	}

	public void tick() {
		Player p = game.getPlayer();
		if(meteors>20){
			meteors = 5;
			meteorhealth +=10;
		}
		time--;

		if (time == 0) {
			for (int j = 0; j <= meteors; j++) {
				addMeteor(new Meteor(r.nextInt(Game.WIDTH * Game.SCALE - 64*Game.SCALE),
						0,meteorhealth, tex,straight));
			}
			time = 300;
			meteors++;
		}
		for (int i = 0; i < b.size(); i++) {
			tempBullet = b.get(i);

			if (tempBullet.getY() < 0 - 40) {
				removeBullet(tempBullet);
			}

			// //////////////collision handling

			for (int j = 0; j < m.size(); j++) {
				tempMeteor = m.get(j);
				// wenn (xb-xm)^2+(yb-ym)^2 < r^2
				if (inCircle(tempBullet.getX(),tempBullet.getY() , tempMeteor.getX(), tempMeteor.getY(), 32)){
						
					// wurde ein treffer erzielt
					removeBullet(tempBullet);
					
					tempMeteor.setHealth(tempMeteor.getHealth()-tempBullet.getDamage());
					if(tempMeteor.getHealth()<=0){
						removeMeteor(tempMeteor);
						addExplosion(new Explosion(tempMeteor.getX(),
								tempMeteor.getY(), tex));
						p.setScore(p.getScore()+meteorhealth);
					} else{
						addExplosion(new SmallExplosion(tempBullet.getX(),
								tempBullet.getY(), tex));
					}
					
				}
			}
			// ///////////////////////////////

			tempBullet.tick();
		}
		
		for (int i = 0; i < sb.size(); i++) {
			tempStrongBullet = sb.get(i);

			if (tempStrongBullet.getY() < 0 - 40) {
				removeStrongBullet(tempStrongBullet);
			}

			// //////////////collision handling

			for (int j = 0; j < m.size(); j++) {
				tempMeteor = m.get(j);
				// wenn (xb-xm)^2+(yb-ym)^2 < r^2
				if (inCircle(tempStrongBullet.getX(),tempStrongBullet.getY() , tempMeteor.getX(), tempMeteor.getY(), 32)){
						
					// wurde ein treffer erzielt
					removeStrongBullet(tempStrongBullet);
					
					tempMeteor.setHealth(tempMeteor.getHealth()-tempStrongBullet.getDamage());
					if(tempMeteor.getHealth()<=0){
						removeMeteor(tempMeteor);
						addExplosion(new Explosion(tempMeteor.getX(),
								tempMeteor.getY(), tex));
						p.setScore(p.getScore()+meteorhealth);
					} else{
						addExplosion(new SmallExplosion(tempStrongBullet.getX(),
								tempStrongBullet.getY(), tex));
					}
					
				}
			}
			// ///////////////////////////////

			tempStrongBullet.tick();
		}
		
		for (int i = 0; i < m.size(); i++) {
			tempMeteor = m.get(i);

			if (tempMeteor.getY() > game.getHeight() + 40) {
				removeMeteor(tempMeteor);
				meteors++;
			} else if(inCircle(tempMeteor.getX(), tempMeteor.getY(), game.getPlayer().getX(), game.getPlayer().getY(), 64-10)){
				removeMeteor(tempMeteor);
				addExplosion(new Explosion(tempMeteor.getX(),
						tempMeteor.getY(), tex));
				game.getPlayer().setHealth(game.getPlayer().getHealth()-tempMeteor.getDamage());
			}
			tempMeteor.tick();
		}
		for (int i = 0; i < e.size(); i++) {
			tempExplosion = e.get(i);

			tempExplosion.tick();

			if (tempExplosion.getTime() == 0) {
				removeExplosion(tempExplosion);
			}
		}
		
		
		if(!messages.isEmpty()){
			messages.get(0).tick();
			if(messages.get(0).getRemainingTicks() <=0){
				messages.remove(0);
			}
		}
	}

	public void render(Graphics g) {
		for (int i = 0; i < b.size(); i++) {
			tempBullet = b.get(i);

			tempBullet.render(g);
		}
		
		for (int i = 0; i < sb.size(); i++){
			tempStrongBullet = sb.get(i);
			
			tempStrongBullet.render(g);
		}
		
		for (int i = 0; i < m.size(); i++) {
			tempMeteor = m.get(i);

			tempMeteor.render(g);
		}
		for (int i = 0; i < e.size(); i++) {
			tempExplosion = e.get(i);

			tempExplosion.render(g);
		}
		
		if(!messages.isEmpty()){
			messages.get(0).render(g);
		}
	}
	
	
	private boolean inCircle(double x, double y, double cx, double cy, double cr){
		return (Math.pow((x - cx), 2) + Math
				.pow((y-cy), 2)) < cr * cr;
	}

	public void addBullet(Bullet block) {
		b.add(block);
	}

	public void removeBullet(Bullet block) {
		b.remove(block);
	}
	
	public void addStrongBullet(StrongBullet block){
		sb.add(block);
	}
	
	public void removeStrongBullet(StrongBullet block){
		sb.remove(block);
	}

	public void addMeteor(Meteor block) {
		m.add(block);
	}

	public void removeMeteor(Meteor block) {
		m.remove(block);
	}

	public void addExplosion(Explosion block) {
		e.add(block);
	}

	public void removeExplosion(Explosion block) {
		e.remove(block);
	}
	
	public void addMessage(Message m){
		messages.add(m);
	}
	
	public int getSize(){
		return b.size()+e.size()+m.size();
	}

}
